package com.xiaoshuidi.cloud.module.report.controller.finance;


import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.report.controller.finance.vo.*;
import com.xiaoshuidi.cloud.module.report.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.report.service.finance.CollectInHandleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.time.LocalDate;
import java.util.Objects;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

/**
 * 收款报表
 */

@Slf4j
@Tag(name = "管理后台 -收款汇总报表")
@RestController
@RequestMapping(ApiConstants.PREFIX + "/collect")
public class CollectHandleReportController {

    @Resource
    private CollectInHandleService collectInHandleService;


    /**
     * 租金及押金收款汇总分页查询
     */
    @PostMapping("/rent/page")
    @Operation(summary = "租金及押金收款汇总分页查询")
    public CommonResult<PageResult<ReportRentDepositlRespVO>> reportRentDepositPage(@RequestBody @Valid CollectHandleReqVO reqVO) {
        log.info("CollectHandleReqVO page req:{}",reqVO.toString());
        return success(collectInHandleService.pageReportRentDepositSummary(reqVO));
    }

    /**
     * 租金及押金收款汇总报表导出
     */
    @PostMapping("/rent/export")
    public CommonResult<ExportRespVO> reportRentDepositExport(@RequestBody @Valid CollectHandleReqVO req) {
        log.info("CollectHandleReqVO export req:{}",req.toString());
        return CommonResult.success(collectInHandleService.reportRentDepositExport(req));
    }

    /**
     * 定金统计分页查询
     */
    @PostMapping("/reserve/page")
    @Operation(summary = "定金统计分页查询")
    public CommonResult<PageResult<ReportReserveRespVO>> reserveSummaryPage(@RequestBody @Valid CollectHandleReqVO reqVO) {
        log.info("CollectHandleReqVO page req:{}",reqVO.toString());
        return success(collectInHandleService.pageReserveSummary(reqVO));
    }

    /**
     * 定金统计导出
     */
    @PostMapping("/reserve/export")
    public CommonResult<ExportRespVO> reserveSummaryExport(@RequestBody @Valid CollectHandleReqVO req) {
        log.info("CollectHandleReqVO export req:{}",req.toString());
        return CommonResult.success(collectInHandleService.reserveSummaryExport(req));
    }

    /**
     * 水电费等收款汇总分页查询
     */
    @PostMapping("/fee/page")
    @Operation(summary = "水电费等收款汇总分页查询")
    public CommonResult<PageResult<ReportFeeRespVO>> feeSummaryPage(@RequestBody @Valid CollectHandleReqVO reqVO) {
        log.info("CollectHandleReqVO page req:{}",reqVO.toString());
        return success(collectInHandleService.pageFeeSummary(reqVO));
    }

    /**
     * 水电费等收款汇总导出
     */
    @PostMapping("/fee/export")
    public CommonResult<ExportRespVO> feeSummaryExport(@RequestBody @Valid CollectHandleReqVO req) {
        log.info("CollectHandleReqVO export req:{}",req.toString());
        return CommonResult.success(collectInHandleService.feeSummaryExport(req));
    }

    /**
     * 所有收款汇总分页查询
     */
    @PostMapping("/all/page")
    @Operation(summary = "所有收款汇总分页查询")
    public CommonResult<PageResult<ReportCollectionSummaryRespVO>> collectionSummaryPage(@RequestBody @Valid CollectHandleReqVO reqVO) {
        log.info("CollectHandleReqVO page req:{}",reqVO.toString());
        return success(collectInHandleService.pageCollectionSummary(reqVO));
    }

    /**
     * 所有收款汇总导出
     */
    @PostMapping("/all/export")
    public CommonResult<ExportRespVO> collectionSummaryExport(@RequestBody @Valid CollectHandleReqVO req) {
        log.info("CollectHandleReqVO export req:{}",req.toString());
        return CommonResult.success(collectInHandleService.collectionSummaryExport(req));
    }

    /**
     * 应收已收租金核对表分页查询
     */
    @PostMapping("/receivable/check/page")
    @Operation(summary = "应收已收租金核对表分页查询")
    public CommonResult<PageResult<ReportReceivableCheckRespVO>> receivableCheckPage(@RequestBody @Valid CollectHandleReqVO reqVO) {
        log.info("CollectHandleReqVO page req:{}",reqVO.toString());
        return success(collectInHandleService.pageReceivableCheck(reqVO));
    }

    /**
     * 应收已收租金核对表导出
     */
    @PostMapping("/receivable/check/export")
    public CommonResult<ExportRespVO> receivableCheckExport(@RequestBody @Valid CollectHandleReqVO req) {
        log.info("CollectHandleReqVO export req:{}",req.toString());
        return CommonResult.success(collectInHandleService.receivableCheckExport(req));
    }


    /**
     * 应收已收租金核对表旧欠分页查询
     */
    @PostMapping("/old/receivable/check/page")
    @Operation(summary = "应收已收租金核对表旧欠分页查询")
    public CommonResult<PageResult<ReportReceivableCheckRespVO>> oldReceivableCheckPage(@RequestBody @Valid CollectHandleReqVO reqVO) {
        log.info("CollectHandleReqVO page req:{}",reqVO.toString());
        LocalDate lastDate = LocalDate.of(2023, 12, 31);
        if(Objects.isNull(reqVO.getEndTime())){
            reqVO.setEndTime(lastDate);
        }else if (reqVO.getEndTime().compareTo(lastDate) > 0){
            reqVO.setEndTime(lastDate);
        }
        return success(collectInHandleService.pageReceivableCheck(reqVO));
    }

    /**
     * 应收已收租金核对表旧欠导出
     */
    @PostMapping("/old/receivable/check/export")
    public CommonResult<ExportRespVO> oldReceivableCheckExport(@RequestBody @Valid CollectHandleReqVO req) {
        log.info("CollectHandleReqVO export req:{}",req.toString());
        LocalDate lastDate = LocalDate.of(2023, 12, 31);
        if(Objects.isNull(req.getEndTime())){
            req.setEndTime(lastDate);
        }else if (req.getEndTime().compareTo(lastDate) > 0){
            req.setEndTime(lastDate);
        }
        return CommonResult.success(collectInHandleService.receivableCheckExport(req));
    }






}
